package com.coocaa.ops.admin.v1.panel.convert;

import com.coocaa.ops.common.core.biz.panel.entity.BlockContentConfigInfo;
import com.coocaa.ops.common.core.biz.panel.entity.VirtuallyBlockInfo;
import com.coocaa.ops.common.tool.base.InstanceUtil;
import lombok.experimental.UtilityClass;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.TreeMap;

/**
 * @author bijiahao
 * @description
 * @date 2020/6/13 12:58
 */
@Slf4j
@UtilityClass
public class BlockQueryConverter {

    public static List<VirtuallyBlockInfo>  convertVirtuallyBlockList(List<BlockContentConfigInfo> commonBlockList, List<BlockContentConfigInfo> dmpBlockList){
        List<VirtuallyBlockInfo> virtuallyBlockInfoList = InstanceUtil.newArrayList();
        TreeMap<Integer, List<BlockContentConfigInfo>> blockContentTreeMap = convertBlockContentTreeMap(commonBlockList);
        boolean isHasDmpBlock = !CollectionUtils.isEmpty(dmpBlockList);
        for (Map.Entry<Integer, List<BlockContentConfigInfo>> entry : blockContentTreeMap.entrySet()){
            VirtuallyBlockInfo virtuallyBlockInfo = new VirtuallyBlockInfo();
            virtuallyBlockInfo.getCommonBlockList().addAll(entry.getValue());
            if (isHasDmpBlock){
                List<BlockContentConfigInfo> filterDmpBlockList =  filterDmpBlockBySeq(entry.getKey(), dmpBlockList);
                virtuallyBlockInfo.getDmpBlockList().addAll(filterDmpBlockList);
            }
            virtuallyBlockInfoList.add(virtuallyBlockInfo);
        }
        return virtuallyBlockInfoList;
    }

    private  List<BlockContentConfigInfo> filterDmpBlockBySeq(Integer blockSeq, List<BlockContentConfigInfo> dmpBlockList){
        List<BlockContentConfigInfo> filterDmpBlockList = InstanceUtil.newArrayList();
        for (BlockContentConfigInfo blockContentConfigInfo : dmpBlockList){
            if(Objects.equals(blockSeq, blockContentConfigInfo.getBlockSeq())){
                filterDmpBlockList.add(blockContentConfigInfo);
            }
        }
        return filterDmpBlockList;
    }

    private static TreeMap<Integer, List<BlockContentConfigInfo>> convertBlockContentTreeMap(List<BlockContentConfigInfo> commonBlockList){
        TreeMap<Integer, List<BlockContentConfigInfo>> blockContentTreeMap = InstanceUtil.newTreeMap();
        for (BlockContentConfigInfo blockContentConfigInfo: commonBlockList){
            Integer blockSeq = blockContentConfigInfo.getBlockSeq();
            if(blockContentTreeMap.containsKey(blockSeq)){
                List<BlockContentConfigInfo> blockContentConfigInfoList = blockContentTreeMap.get(blockSeq);
                blockContentConfigInfoList.add(blockContentConfigInfo);
            }else {
                List<BlockContentConfigInfo> newBlockContentConfigInfoList = InstanceUtil.newArrayList();
                newBlockContentConfigInfoList.add(blockContentConfigInfo);
                blockContentTreeMap.put(blockSeq, newBlockContentConfigInfoList);
            }
        }
        return blockContentTreeMap;
    }
}
